﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;
using TuanAnhPersonalWeb.Classes;
using MvcPaging;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,content")]
    public class ContentController : Controller
    {
        private TuanAnhEntities db = new TuanAnhEntities();
        ContentAccessConsolidator access = new ContentAccessConsolidator();
        private const int defaultPageSize =3;

        //
        // GET: /Admin/Content/
        
        public ActionResult Index(int? page)
        {
            ViewData["Type"] = new SelectList(access.getAllType(), "TYPE_ID", "DESCRIPTION");
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            IList<ContentModel> list = access.getallContent().Where(e => e.TYPE_ID == 2).OrderByDescending(e=>e.CREATEDATE).ToList();                                                             
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list.ToPagedList(currentPageIndex, defaultPageSize));
            }
            else
            {
                return View(list.ToPagedList(currentPageIndex, defaultPageSize));
            }
        }

         public ActionResult Search(int? page, string keySearch = "code", string valSearch = "", int? TYPE_ID = 2)
         {
             ViewData["keySearch"] = keySearch;
             ViewData["valSearch"] = valSearch;
             ViewData["TypeID"] = TYPE_ID;
             int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
             IList<ContentModel> list  = access.getallContent().OrderByDescending(e=>e.CREATEDATE).ToList();
             list = list.Where(e => e.CONTENT_TITLE.ToLower().Contains(valSearch.ToLower()) && e.TYPE_ID == TYPE_ID).ToList().ToPagedList(currentPageIndex, defaultPageSize);
             if (Request.IsAjaxRequest())
             {
                 return PartialView("_ListControl", list);
             }
             else
             {
                 return View("Index", list);
             }

         }

        //
        // GET: /Admin/Content/Details/5
        
        public ViewResult Details(int id)
        {
            TUANANH__CONTENT tuananh__content = db.TUANANH__CONTENT.Single(t => t.CONTENT_ID == id);
            return View(tuananh__content);
        }

        //
        // GET: /Admin/Content/Create
      
        public ActionResult Create()
        {
            ViewData["Type"] = new SelectList(access.getAllType(), "TYPE_ID", "DESCRIPTION");
            return View();
        } 

        //
        // POST: /Admin/Content/Create

        [HttpPost]
        [ValidateInput(false)]
       
        public ActionResult Create(ContentModel model)
        {
            if (ModelState.IsValid)
            {
                access.InsertContent(model);
                return RedirectToAction("Index");  
            }

            return View(model);
        }
        
        //
        // GET: /Admin/Content/Edit/5
         
        public ActionResult Edit(int id)
        {
            ContentModel data= access.getcontetByID(id);
            ViewData["Type"] = new SelectList(access.getAllType(), "TYPE_ID", "DESCRIPTION",data.TYPE_ID);
            return View(data);
        }

        //
        // POST: /Admin/Content/Edit/5

        [HttpPost]
        [ValidateInput(false)]
       
        public ActionResult Edit(ContentModel item)
        {
            if (ModelState.IsValid)
            {
                access.editImage(item);
                return RedirectToAction("Index");
            }
            ViewData["Type"] = new SelectList(access.getAllType(), "TYPE_ID", "DESCRIPTION", item.TYPE_ID);
            return View(item);
        }

        //
        // GET: /Admin/Content/Delete/5
       
        public ActionResult Delete(int id)
        {
            TUANANH__CONTENT tuananh__content = db.TUANANH__CONTENT.Single(t => t.CONTENT_ID == id);
            return View(tuananh__content);
        }

        //
        // POST: /Admin/Content/Delete/5
       
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TUANANH__CONTENT tuananh__content = db.TUANANH__CONTENT.Single(t => t.CONTENT_ID == id);
            db.TUANANH__CONTENT.DeleteObject(tuananh__content);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
       
        public ActionResult HomePage(int id)
        {
            access.updateEventToHome(id);
            return View("Success");
        }
       
        public ActionResult updateDate(int id)
        {
            access.updateDate(id);
            return View("Success");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}